<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-header">
        <svg t="1735727532209" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
          p-id="8148" id="mx_n_1735727532210" width="60" height="60">
          <path
            d="M118.53979 672.314219c45.079708-255.730359 160.959925-350.633822 298.097282-388.718209 171.630962-47.799656 321.445036-13.525038 466.207048-81.648578 87.461981-41.158399 83.854826 32.720216 82.316797 44.588521-22.147416 171.003676-112.654756 333.892533-306.120003 411.215596-211.012901 84.290755-419.269016-71.777767-570.344826 173.733856-1.112334 1.807159-3.077082 2.920516-5.199419 2.920516L64.426335 834.405921c-4.547573 0-7.488555-4.796236-5.443989-8.857738C115.666346 712.971198 197.387579 616.041588 301.715693 538.76662c94.010117-69.554122 206.484771-123.56013 335.51345-166.600389 10.44591-3.484358 6.036483-19.138896-4.687766-16.639982-98.93836 23.053042-188.342576 53.163827-271.330662 100.467179-86.72827 49.427736-166.504198 117.746728-242.667855 216.320791L118.53979 672.314219zM118.53979 672.314219"
            fill="#18a058" p-id="8149"></path>
        </svg>
        <h2>HEALTH LIFE</h2>
        <p class="subtitle">健康生活管理系统</p>
      </div>

      <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" :prefix-icon="User" size="large" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" :prefix-icon="Lock" size="large"
            show-password />
        </el-form-item>

        <div class="remember-me">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary">忘记密码？</el-link>
        </div>

        <el-button type="primary" class="login-button" size="large" :loading="loading" @click="handleLogin">
          登录
        </el-button>
      </el-form>
    </div>

    <div class="login-footer">
      <p>Copyright © 2024 Health Life. All rights reserved.</p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const formRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true // 加载动画开启
    await userStore.login(loginForm)
  } catch (error) {
    console.error('登录验证失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
}

.login-content {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 28px;
  color: #333;
  margin: 0;
  font-weight: 600;
}

.subtitle {
  margin-top: 8px;
  color: #666;
  font-size: 16px;
}

.login-form {
  margin-top: 24px;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 0 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  background: linear-gradient(to right, #18a058, #36ad6a);
  border: none;
}

.login-button:hover {
  background: linear-gradient(to right, #0a8c4c, #2a9d5d);
}

.login-footer {
  margin-top: 24px;
  color: #666;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  padding: 4px 11px;
}

:deep(.el-input__inner) {
  height: 42px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
}
</style>
